package com.yuxin.tongyi.lsll.util;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;
import java.util.Stack;




/**
 * android http 请求
 * @author zlh
 *
 */
public class HttpRequestUtil {

	public static final String ServerUrl = "http://218.206.201.198:8082/ywtws-core-web/";
	//public static final String ServerUrl = "http://125.40.85.62:8088/ywtws-core-web/";
	//public static final String ServerUrl = "http://192.168.32.172:8080/ywtws-core-web/";
	
	/* 公用基础资料 */
	private static final String classListUrl = "/getClass.do";   //班级列表url
	private static final String eventListUrl = "/getEventList.do";   //事件列表url
	private static final String babyListUrl = "/getStudent.do";    //学生列表url
	/* 登录模块 */
	private static final String loginUrl = "/UserLogin.do";   //登陆url
	private static final String versionUrl = "/getVersion.do"; //验证版本信息
	/* 拍照上传模块 */
	public static final String uploadUrl = "saveTeacherGrowUp.do";  //保存成长记录
	/* 一日生活模块 */
	private static final String daylifeUrl = "/getDayLife.do"; //获取学生的一日生活
	private static final String senddaylifeinfo = "/sendDayLifeInfo.do";  //发送一日生活信息
	/* 通知发送模块 */
	private static final String noticeSendMsgUrl = "/noticeSendMsg.do"; //通知发送url
	
	private String status;                 //请求状态
	private List<Object> objList = null;
	private Stack<Object> objStack = null;


	
	/**
	 * 请求班级列表 
	 * @param loginName
	 * @param passWord
	 * @return
	 */
	public boolean classListRequest(String loginName,String passWord) {
		try{
			String httpUrl = ServerUrl+classListUrl+"?loginName="+loginName+"&pwd="+passWord;
			return RequestUrl(httpUrl);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 请求事件列表 
	 * @param loginName
	 * @param passWord
	 * @return
	 */
	public boolean eventListRequest(String loginName,String passWord) {
		try{
			String httpUrl = ServerUrl+eventListUrl+"?loginName="+loginName+"&pwd="+passWord;
			return RequestUrl(httpUrl);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 请求学生列表 
	 * @param loginName
	 * @param passWord
	 * @return
	 */
	public boolean babyListRequest(String loginName,String passWord,String classId) {
		try{
			String httpUrl = ServerUrl+babyListUrl+"?loginName="+loginName+"&pwd="+passWord+"&classId="+classId;
			return RequestUrl(httpUrl);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 登陆请求方法
	 * @param loginName
	 * @param passWord
	 * @return
	 */
	public boolean LoginRequest(String loginName,String passWord) {
		try{
			String httpUrl = ServerUrl+loginUrl+"?loginName="+loginName+"&passWord="+passWord;
			return RequestUrl(httpUrl);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 版本请求方法
	 * @return
	 */
	public boolean validateVersion() {
		try{
			String httpUrl = ServerUrl+versionUrl;
			return RequestUrl(httpUrl);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return false;
	}


	/**
	 * 一日生活请求
	 * @param loginName
	 * @param passWord
	 * @param queryTime
	 * @param classId
	 * @return
	 */
	public boolean requestDayLife(String loginName, String passWord, String queryTime, String stuId, String classId) {
		try{
			String httpUrl = ServerUrl + daylifeUrl + "?loginName=" + loginName + "&passWord=" + passWord;
			if(null!=queryTime && !"".equals(queryTime)) {
				httpUrl += "&queryTime="+queryTime;
			}
			if(null!=stuId && !"".equals(stuId)){
				httpUrl += "&stuId="+stuId;
			}
			if(null!=classId && !"".equals(classId)){
				httpUrl += "&classId="+classId;
			}
		    return RequestUrl(httpUrl);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 一日生活发送请求
	 * @param loginName
	 * @param passWord
	 * @param queryTime
	 * @param classId
	 * @return
	 */
	public boolean requestSendDayLife(String loginName, String passWord, String classId, String jsonStr) {
		try{
			jsonStr = URLEncoder.encode(jsonStr, "UTF-8");
			String httpUrl = ServerUrl + senddaylifeinfo + "?loginName=" + loginName + "&passWord=" + passWord;
			
			if(null!=classId && !"".equals(classId)){
				httpUrl += "&classId="+classId;
			}
//			if(null!=jsonStr && !"".equals(jsonStr)){
//				httpUrl += "&jsonStr="+jsonStr;
//			}
			byte[] b = null ;
			if(null!=jsonStr && !"".equals(jsonStr)){
				b = jsonStr.getBytes();
			}
			HttpURLConnection conn = null;
			URL url = new URL(httpUrl);
			conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("POST");
			conn.setRequestProperty("content-type", "text/html");
			conn.setRequestProperty("Accept-Charset", "utf-8");
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setRequestProperty("Content-Length", String.valueOf(b.length));
			OutputStream outStream = conn.getOutputStream();  
	        outStream.write(b);  
	        outStream.flush();  
	        outStream.close();  
			
	        int res = conn.getResponseCode();
			if (res == 200) {
				InputStream u = conn.getInputStream();//
				XMLUtil xmlUtil = new XMLUtil();
				status = xmlUtil.readServerXML(u);
				objList = xmlUtil.getObjList();
				objStack = xmlUtil.getObjStack();
				
				return true;
			} else {
				// 请求服务器出错
				return false;
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	

	/**
	 * 通知发送请求
	 * @param loginName
	 * @param passWord
	 * @param msgContent 
	 */
	
	public boolean noticeSendRequest(String loginName,String passWord,String msgContent,String classId){
		try{
			msgContent = URLEncoder.encode(msgContent, "UTF-8");
			String httpUrl = ServerUrl+noticeSendMsgUrl+"?loginName="+loginName+"&passWord="+passWord+"&msgContent="+msgContent+"&classId="+classId;
			return RequestUrl(httpUrl);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	
	/**
	 * 请求方法
	 * 
	 * @param url
	 */
	public boolean RequestUrl(String requestUrl) {
		HttpURLConnection conn = null;
		try {
			URL url = new URL(requestUrl);
			conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("POST");
			conn.setRequestProperty("content-type", "text/html");
			conn.setRequestProperty("Accept-Charset", "utf-8");
			conn.setDoOutput(true);
			conn.setDoInput(true);
			// c.setConnectTimeout(30000);//设置连接主机超时（单位：毫秒）
			// c.setReadTimeout(30000);//设置从主机读取数据超时（单位：毫秒）
			conn.connect();
			String header;
			for (int i = 0; true; i++) {
				header = conn.getHeaderField(i);
				if (header == null)
					break;
			}
			int res = conn.getResponseCode();
			if (res == 200) {
				InputStream u = conn.getInputStream();//
				XMLUtil xmlUtil = new XMLUtil();
				status = xmlUtil.readServerXML(u);
				objList = xmlUtil.getObjList();
				objStack = xmlUtil.getObjStack();
				// BufferedReader in = new BufferedReader(new
				// InputStreamReader(u));
				// buffer = new StringBuffer();
				// String line = "";
				// while ((line = in.readLine()) != null) {
				// buffer.append(line);
				// }
				return true;
			} else {
				// 请求服务器出错
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			conn.disconnect();
		}
		return false;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}
	
	public Stack<Object> getObjStack() {
		return objStack;
	}
	
	public List<Object> getObjList() {
		return objList;
	}
}
